package com.example.cloudproviderorder80.controller;

import com.example.api.entity.CommonResult;
import com.example.api.entity.Payment;
import com.example.cloudproviderorder80.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("order")
public class OrderController {
    public static final String URL = "http://CLOUD-PAYMENT-SERVICE/payment/{id}";
    private final RestTemplate restTemplate;
    private final DiscoveryClient discoveryClient;

    private final PaymentFeignService paymentFeignService;

    public OrderController(DiscoveryClient discoveryClient,
                           PaymentFeignService paymentFeignService,
                           RestTemplate restTemplate) {
        this.discoveryClient = discoveryClient;
        this.paymentFeignService = paymentFeignService;
        this.restTemplate = restTemplate;
    }

    @GetMapping("/{id}")
    public CommonResult getPaymentById(@PathVariable Long id) {
        Map<String, String> request = new HashMap<>();
        request.put("id", String.valueOf(id));

        return restTemplate.getForObject(URL, CommonResult.class, request);
    }

    @GetMapping("/info")
    public CommonResult getinfo() {
        return new CommonResult(200, "成功", discoveryClient);
    }

    @GetMapping("/paymentFeignService")
    public CommonResult<Payment> getinfo(@RequestParam("id") Long id) {
        return paymentFeignService.getPaymentById(id);
    }

    @GetMapping("/feignTimeout")
    public String feignTimeout() {
        return paymentFeignService.feignTimeout();
    }

    @GetMapping("/feignException")
    public String feignException() {
        return paymentFeignService.feignException();
    }
}
